define(function(require, exports, module) {
    "use strict";
    var $ = require("$"),
        app = require("dist/application/app"),
        Monkey = require("./monkey"),
        config = app.config,
        method = app.method;
    require("./jq_pagination");
    var item_tmp = require("./template/item.html");
    var item_tmp2 = require("./template/item2.html");
    exports.init = function(el) {
        var SelecteFun = function(opts) {
            this.opts = $.extend(true, {
                modal: null,
                type: null,
                select: null,
                goodsurl: "/Plus/selectgoods.php"
            }, opts || {});
            this.init();
        }

        SelecteFun.prototype = {
            init: function() {
                var _this = this;
                _this.create({
                    "page": 1
                });
                _this._click();
                _this.searchF();
            },
            create: function(searchData) {
                var _this = this;
                Monkey.post(_this.opts.goodsurl, function(d) {
                    if (config.issucceed(d)) {
                        if (_this.opts.type == window.goods_set.type) {
                            d.data.selectid = _this.opts.select.val();
                        }else{
                            var selectid=$(".js_goods_id",$(_this.opts.modal.attr("data-target"))).val();
                            if(selectid){
                                d.data.selectid =selectid;
                            }
                        }
                        $('#all-list-box').html(template.compile(item_tmp)(d.data));
                        var initPagination = function() {
                            // 创建分页
                            $("#Pagination").pagination(d.data.chief, {
                                num_edge_entries: 1, //边缘页数
                                num_display_entries: 4, //主体页数
                                callback: pageselectCallback,
                                items_per_page: 1, //每页显示1项
                                prev_text: "&laquo",
                                next_text: "&raquo",
                                load_first_page: true //第一次自动加载,true(默认),false(不加载)
                            });
                        };

                        var pageselectCallback = function(page_index, jq) {
                            var data = $("form.search-form", _this.opts.modal).serialize();
                            data += "&page=" + (page_index+1) + "";
                            if (!$('#all-list-box div.list-box:eq(' + page_index + ')').data("crate")) {
                                Monkey.postd(_this.opts.goodsurl, function(d) {
                                    if (config.issucceed(d)) {
                                        if (_this.opts.type == window.goods_set.type) {
                                            d.data.selectid = _this.opts.select.val();
                                        }
                                        var $html = template.compile(item_tmp2)(d.data);
                                        $('#all-list-box').html($html);
                                    } else {
                                        config.msg.error(d.message);
                                    }
                                }, data);
                            }
                            return false;
                        }
                        initPagination();
                    } else {
                        config.msg.error(d.message);
                    }

                }, '', null, searchData);
            },
            _click: function() {},
            searchF: function() {
                var _this = this;
                $(document).on("click", "#goodssearch", function() {
                    var data = $("form.search-form", _this.opts.modal).serialize();
                    data += "&page=1";
                    _this.create(data);
                })
            }

        }
        var $selectGoods = $("[data-toggle='selectGoods']", el);
        $selectGoods.length > 0 && $selectGoods.each(function() {
            var _this = $(this);
            _this.on("click", function() {
                $("li.itmes").unbind("click");
                $("button.js_save_btn").unbind("click");
                var $this = $(this),
                    $tmodal = $($this.data("tmodal")).length > 0 ? $($this.data("tmodal")) : $("#js_goodes_modal"),
                    type = $this.data("type"),
                    $select = $($this.data("selectid")).length > 0 ? $($this.data("selectid")) : $("#js_selected_id"),
                    $target = $this.data("target"),
                    path = $this.data("path"),
                    $trid = $("#js_tr_id"),
                    trid = $this.data("id");
                $tmodal.attr("data-type", type);
                $tmodal.attr("data-target", $target);
                $tmodal.attr("data-select", $this.data("selectid") || "#js_selected_id");
                $tmodal.attr("data-logo", $this.data("logo"));
                var $input = $("#js_selected_id", $tmodal);
                $trid.length > 0 && $trid.val(trid);
                $input.val($(".js_goods_id", $target).val() || $this.data("goodsid"));
                if ($input.val() == "") {
                    $input.val(window.goods_set.selectid);
                }

                $tmodal.modal("show");
                if (path) {
                    new SelecteFun({
                        modal: $tmodal,
                        type: type,
                        button: $this,
                        select: $select,
                        goodsurl: path
                    });
                }
                $($tmodal, document).on("click", "li.itmes", function() {
                    var cid = $(this).data("cid");
                    var $select = $($tmodal.attr("data-select"));
                    $("li.itmes", $tmodal).removeClass('active');
                    $("li.itmes", $tmodal).find("a").removeClass('active');
                    var activeItme = $(".goods_items_" + cid);
                    activeItme.find("a").toggleClass("active");
                    activeItme.toggleClass("active");
                    $select.val(cid);

                });
                $($tmodal, document).on("click", "button.js_save_btn", function() {
                    $tmodal.modal("hide");
                    var $select = $($tmodal.attr("data-select")),
                        target = $tmodal.attr("data-target"),
                        id = $select.val(),
                        $goods = $(".goods_items_" + id),
                        url = $goods.data("url"),
                        imgsrc = $goods.data("imgsrc"),
                        logo = $goods.data("logo"),
                        text = $goods.data("text"),
                        $logo = $($tmodal.attr("data-logo")),
                        type = $tmodal.attr("data-type");
                    $(target).find("input[type='hidden']").val(id);
                    if (imgsrc) {
                        if (type == "url") {
                            $(target).find("input[type='text']").val(url);
                        } else if (type == "goods") {
                            $(target).removeClass('hide');
                            $(target).find("img").attr("src", imgsrc);
                            $(target).find("p").html(text);
                        }
                        if (imgsrc) {
                            $logo.find(".prev-logo").attr("src", imgsrc).removeClass('hide');
                            $logo.find("input[type='text'].hide").attr("value", imgsrc);
                            $logo.find(".addlogo[data-toggle='selectimg']").addClass('hide');
                            $logo.find("button.cancleLogo").removeClass('hide');
                        }
                    }
                });
            });
            var $prev = _this.prev("input[type='text']");
            $prev.length > 0 && $prev.on("click", function() {
                _this.trigger("click");
            });

        });

    }
});